package yf.mall.controller.api;

import com.yf.annotation.IgnoreAuth;
import com.yf.entity.TokenEntity;
import com.yf.entity.WxUser;
import com.yf.response.R;
import com.yf.response.RRException;
import com.yf.service.TokenService;
import com.yf.service.WxAppService;
import org.springframework.web.bind.annotation.RequestParam;
import yf.mall.entity.UserEntity;
import yf.mall.service.UserService;
import com.yf.service.WxWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RequestMapping("/api/login")
@RestController
public class ApiLoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private WxWebService wxWebService;

    @Autowired
    private WxAppService wxAppService;

    @RequestMapping("/code/url")
    @IgnoreAuth
    public R codeUrl(String backUrl) {
        String url = wxWebService.getCodeUrl(backUrl);
        return R.ok(url);
    }

    /**
     * 手机号登录
     *
     * @param code
     * @return
     */
    @RequestMapping("/phone")
    @IgnoreAuth
    public R h5Login(String name, String avatar, String openId, String unionId, String phone, String captcha, Long topUserId, Boolean register) {
        TokenEntity tokenEntity = userService.phoneLogin(avatar, name, openId, unionId, phone, captcha, topUserId, register);
        return R.ok(tokenEntity);
    }

    /**
     * 微信网页登录
     *
     * @param code
     * @return
     */
    @RequestMapping("/wx")
    @IgnoreAuth
    public R h5Login(String code, Long topUserId, Boolean register) {
        WxUser wxUser = wxWebService.getWxUser(code);
        //查找微信用户
        TokenEntity tokenEntity = userService.wxLogin(wxUser, topUserId, register);
        return R.ok(tokenEntity).putExtra("wxUser", wxUser);
    }

    /**
     * 小程序登录
     *
     * @return
     */
    @RequestMapping("/wx/app")
    public R wxLogin(String code) {
        WxUser wxUser = wxAppService.getBaseWxUser(code);
        String wxId = wxUser.getOpenId();
        if (wxUser.getUnionId() != null) {
            wxId = wxUser.getUnionId();
        }
        UserEntity userEntity = userService.findByWxId(wxId);
        if (userEntity != null) {
            TokenEntity tokenEntity = tokenService.createToken(userEntity.getId());
            return R.ok(tokenEntity).putExtra("wxUser", wxUser);
        }
        return R.ok().putExtra("wxUser", wxUser);
    }

    /**
     * 小程序注册 TODO 实现注册
     *
     * @return
     */
    @RequestMapping("/wx/register")
    @IgnoreAuth
    public R wxRegister(String openId, String unionId, Long topUserId, String avatar, String name) {
        UserEntity userEntity = userService.register(openId, unionId, name, avatar, null, null, topUserId);
        return R.ok(userEntity);
    }



    /**
     * 测试登录
     *
     * @param userId
     * @return
     */
    @RequestMapping("/test")
    @IgnoreAuth
    public R testLogin(Long userId) {
//        userService.register("22","22","测试","",userId);
        UserEntity userEntity = userService.get(userId);
        if (userEntity == null) {
            WxUser wxUser = new WxUser();
            wxUser.setUnionId("奥术大师大所多");
            wxUser.setOpenId("asd");
            return R.ok().putExtra("wxUser", wxUser);
        }
        TokenEntity tokenEntity = tokenService.createToken(userId);
        return R.ok(tokenEntity);
    }


}
